/**
 * TimelineHelper
 */

  var TimelineHelper = (function (){
      var colorIndex = 0;
      // 预定义触发动效颜色值
      var colors = [
          "f8b537", "c7c020", "9cb800", "57b000", "2fbe82",
          "26c6ce", "22b5ff", "377ff8", "8037f8", "8a00cd",
          "b411a3", "ee0052", "b91f1f", "f87a37", "a8997c",
          "85a87c", "7ca0a8", "9f7ca8", "a87c7c", "595b64"
      ];

      var rebuildTimeline = function (dataProvider, timeline, timelineWidget){
          var sourceTimeline = dataProvider.getSpecifyTimelineById(timelineWidget.id, timeline.id);

          var _timeline = {};
          _timeline.id = timeline.id;
          _timeline.label = timelineWidget.label;
          _timeline.widget_id = timelineWidget.id;
          _timeline.icon_class = timelineWidget.icon_class;
          _timeline.display = timeline.an_types[0].an_type_display;
          _timeline.triggered_by = timeline.triggered_by || (sourceTimeline ? (sourceTimeline.triggered_by ? sourceTimeline.triggered_by : []) : []);
          _timeline.start_time = timeline.start_time;
          _timeline.end_time = timeline.end_time;
          return _timeline;
      };

      var filterTimeline = function (dataProvider, timeline, timelineWidget, triggerWidget){
          if (timelineWidget){
              var _timeline = rebuildTimeline(dataProvider, timeline, timelineWidget);
              var triggered_by = timeline.triggered_by || [];
              if (triggered_by.length){
                  if (triggered_by.indexOf(triggerWidget.id) != -1){
                      _timeline.selected = true;
                      return _timeline;
                  }
                  for(var i in triggered_by){
                      var id = triggered_by[i];
                      var timelineWidget = dataProvider.getSpecifyWidgetById(id);
                      if(!timelineWidget) return _timeline;
                  }
                  return null;
              }
              return _timeline;
          }
      };

      return {
          filterTimelines: function (dataProvider, timelines, triggerWidget){
              var filterTimelines = [];
              for (var widgetID in timelines){
                  if (timelines.hasOwnProperty(widgetID)){
                      var _timelines = timelines[widgetID];
                      var timelineWidget = dataProvider.getSpecifyWidgetById(widgetID);
                      _timelines.forEach(function (timeline){
                          var _filterTimeline = filterTimeline(dataProvider, timeline, timelineWidget, triggerWidget);
                          _filterTimeline && (filterTimelines.push(_filterTimeline));
                      });
                  }
              }
              return filterTimelines;
          },
          getTriggerColor: function (){
              var color = colors[colorIndex++];
              if (colorIndex >= colors.length){
                  colorIndex = 0;
              }
              return "#" + color;
          }
      }
  })();

  module.exports = TimelineHelper;
